package com.xinqi.modules.discuss.discuss.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xinqi.common.base.dto.CommonFileDTO;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.base.utils.spring.Spring;
import com.xinqi.common.core.enums.AssistantMessageTypeEnum;
import com.xinqi.common.core.enums.ClassRoleEnum;
import com.xinqi.common.core.enums.CourseContentTypeEnum;
import com.xinqi.common.core.enums.DiscussReleaseStatusEnum;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.course.chapter.dto.client.CourseChapterClient;
import com.xinqi.modules.course.classmenu.client.ClassMenuClient;
import com.xinqi.modules.course.clazz.client.ClassStudentClient;
import com.xinqi.modules.course.clazz.dto.rsp.ClassStudentResultDTO;
import com.xinqi.modules.course.content.dto.client.CourseContentClient;
import com.xinqi.modules.course.content.dto.req.CourseContentCreateDTO;
import com.xinqi.modules.course.content.dto.req.CourseContentUpdateDTO;
import com.xinqi.modules.course.content.dto.rsp.CourseContentResultDTO;
import com.xinqi.modules.course.course.constant.CourseConstants;
import com.xinqi.modules.discuss.assistant.AssistantListen;
import com.xinqi.modules.discuss.discuss.convert.CourseDiscussConvert;
import com.xinqi.modules.discuss.discuss.domain.CourseDiscussEntity;
import com.xinqi.modules.discuss.discuss.domain.CourseDiscussStudentLogEntity;
import com.xinqi.modules.discuss.discuss.dto.req.*;
import com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussResultDTO;
import com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussStatisticsResultDTO;
import com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussStudentLogResultDTO;
import com.xinqi.modules.discuss.discuss.mapper.CourseDiscussMapper;
import com.xinqi.modules.discuss.discuss.service.CourseDiscussService;
import com.xinqi.modules.discuss.discuss.service.CourseDiscussStudentLogService;
import com.xinqi.modules.stats.assistant.dto.rsp.AssistantTotalResultDTO;
import com.xinqi.modules.stats.assistant.dto.rsp.StudentInfoResultDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 课程讨论主表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/03/13
 */
@Log4j2
@Service("courseDiscussService")
@RequiredArgsConstructor
public class CourseDiscussServiceImpl extends BaseServiceImpl<CourseDiscussMapper, CourseDiscussEntity> implements CourseDiscussService {

    private final CourseDiscussConvert courseDiscussConvert;

    private final CourseDiscussStudentLogService courseDiscussStudentLogService;

    private final ClassStudentClient classStudentClient;

    private final CourseContentClient courseContentClient;

    private final CourseChapterClient courseChapterClient;

    private final ClassMenuClient classMenuClient;

    private final AssistantListen assistantListen;

    /**
     * 分页查询 -- 带统计数据
     *
     * @Title:
     * @MethodName: selectPage
     * [pageable, query]
     * @Return com.xinqi.common.base.response.PageInfo<com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/17 9:57
     */
    @Override
    @DS("slaver")
    public PageInfo<CourseDiscussResultDTO> selectPage(Param pageable, CourseDiscussQueryDTO query) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(query.getClassId()), MessageUtils.message("course.discuss.classId.not.blank"));
        //排序
        if (CollectionUtils.isEmpty(pageable.getSorts())) {
            pageable.setSorts(new ArrayList<>());
        }
        pageable.getSorts().add("t1.is_top,desc");
        pageable.getSorts().add("t1.top_time,desc");
        pageable.getSorts().add("t1.release_time,desc");
        //已发布
        if (Objects.equals(1, query.getType())) {
            query.setStatusList(Arrays.asList(DiscussReleaseStatusEnum.IN_PROGRESS.getCode(), DiscussReleaseStatusEnum.CLOSED.getCode()));
        }
        //未发布
        if (Objects.equals(2, query.getType())) {
            query.setStatusList(Arrays.asList(DiscussReleaseStatusEnum.UNPUBLISHED.getCode(), DiscussReleaseStatusEnum.SCHEDULED_RELEASE.getCode()));
            pageable.setSorts(new ArrayList<>());
            pageable.getSorts().add("t1.create_time,desc");
        }

        // 获取主学生信息
        ClassStudentResultDTO mainStudent = R.as(classStudentClient.findVirtualClassStudentByClassIdAndUserId(query.getClassId(), query.getUserId()));
        if (Objects.nonNull(mainStudent)) {
            query.setUserId(mainStudent.getStudentId());
        }

        IPage<CourseDiscussResultDTO> pageData = this.baseMapper.selectPage1(Pages.page(pageable), query);
        return Pages.convert(pageData);
    }


    /**
     * 根据用户和讨论ID获取讨论信息及用户的参与信息
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public CourseDiscussResultDTO findByIdAndUserId(Long id, Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.discuss.id.not.blank"));
        CourseDiscussResultDTO result = Optional.ofNullable(this.baseMapper.selectByIdAndUserId(id, userId, CourseContentTypeEnum.DISCUSS.getCode())).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));

        classMenuClient.checkShowCourseContent(result.getClassId(), CourseContentTypeEnum.DISCUSS);

        //查看即已读
        courseDiscussStudentLogService.synchroRead(result, userId);
        if (Objects.isNull(result.getChapterId())) {
            return result;
        } else if (!Objects.equals(0L, result.getChapterId())) {
            String name = R.as(courseChapterClient.selectName(result.getChapterId()));
            result.setChapterName(name);
        }
        return result;
    }

    /**
     * 添加讨论
     *
     * @Title:
     * @MethodName: insert
     * [dto]
     * @Return java.lang.Long
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 15:10
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insert(CourseDiscussCreateDTO dto) {

        ClassStudentResultDTO studentResult = R.as(classStudentClient.checkRole(dto.getClassId(), dto.getOperationUserId(), new ArrayList<>()));

        if (ClassRoleEnum.STUDENT.getCode().equals(studentResult.getClassRole())
            || ClassRoleEnum.REPRESENTATIVE.getCode().equals(studentResult.getClassRole())) {
            //学生：创建讨论没有配置等限制信息，创建即发布
            dto.setSynchro(YesNoNumberEnum.NO.getCode());
            dto.setClassIds(null);
            dto.setIsReplay(YesNoNumberEnum.NO.getCode());
            dto.setTiming(YesNoNumberEnum.NO.getCode());
            dto.setReleaseTime(null);
            dto.setCommentView(YesNoNumberEnum.NO.getCode());
            dto.setNowRelease(YesNoNumberEnum.YES.getCode());
        }
        if (Objects.isNull(dto.getTiming())) {
            dto.setTiming(YesNoNumberEnum.NO.getCode());
        }

        CourseDiscussEntity entity = courseDiscussConvert.create(dto);
        if (Objects.equals(ClassRoleEnum.TEACHER.getCode(), studentResult.getClassRole()) || Objects.equals(ClassRoleEnum.ASSISTANT.getCode(), studentResult.getClassRole())) {
            entity.setIsTeacher(YesNoNumberEnum.YES.getCode());
        }
        //预设未发布
        entity.setStatus(YesNoNumberEnum.NO.getCode());
        LocalDateTime now = LocalDateTime.now();
        //校验当前时间在截止时间之前
        CommonErrors.BAD_REQUEST.check(now.isBefore(dto.getEndTime()), MessageUtils.message("course.discuss.now.before"));
        //定时发布
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getTiming())) {
            CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto.getReleaseTime()), MessageUtils.message("course.discuss.releasetime.not.blank"));
            //校验定时发布的发布时间在截止时间之前
            CommonErrors.BAD_REQUEST.check(now.isBefore(dto.getReleaseTime()), MessageUtils.message("course.discuss.releasetime.before"));
            CommonErrors.BAD_REQUEST.check(dto.getReleaseTime().isBefore(dto.getEndTime()), MessageUtils.message("course.discuss.releasetime.before"));
            entity.setStatus(YesNoNumberEnum.NO.getCode());
            entity.setNowRelease(YesNoNumberEnum.NO.getCode());
        }
        //立即发布
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getNowRelease())) {
            entity.setStatus(YesNoNumberEnum.YES.getCode());
            entity.setReleaseTime(now);
            entity.setTiming(YesNoNumberEnum.NO.getCode());
        }
        entity.setIsTop(YesNoNumberEnum.NO.getCode());
        entity.setIsSuper(YesNoNumberEnum.NO.getCode());
        entity.setIsDelete(YesNoNumberEnum.NO.getCode());
        entity.setCreateBy(dto.getOperationUserId());
        this.save(entity);
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getNowRelease())) {
            //TODO 发送消息助手
            Spring.committed(() -> assistantListen.releaseCourseContent(entity.getId(), AssistantMessageTypeEnum.DISCUSS));
            //同步班级设置
            this.synchroClass(entity);
        }
        // 同步主表设置
        this.synchroContentDiscuss(courseDiscussConvert.convert(entity));
        //同步学生设置
        courseDiscussStudentLogService.synchroStudent(entity);
        return true;
    }

    /**
     * 编辑讨论
     *
     * @Title:
     * @MethodName: updateById
     * [dto]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/15 9:34
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateById(CourseDiscussUpdateDTO dto) {
        String message = MessageUtils.message("course.discuss.update.success");
        LocalDateTime now = LocalDateTime.now();
        CourseDiscussEntity old = Optional.ofNullable(this.getById(dto.getId())).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));

        //校验为本班学生
        classStudentClient.checkRole(dto.getClassId(), dto.getOperationUserId(), new ArrayList<>());
        //已结束的不可以编辑
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), old.getStatus())) {
            CommonErrors.BAD_REQUEST.check(now.isBefore(dto.getEndTime()), MessageUtils.message("course.discuss.update.closed"));
        }
        //身份校验，自己对自己讨论有编辑
        CommonErrors.BAD_REQUEST.check(Objects.equals(dto.getOperationUserId(), old.getCreateBy()), MessageUtils.message("course.discuss.is.not.own"));

        //校验当前时间在截止时间之前
        CommonErrors.BAD_REQUEST.check(now.isBefore(dto.getEndTime()), MessageUtils.message("course.discuss.now.before"));
        CourseDiscussEntity entity = null;
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), old.getStatus())) {
            //进行中只可以修改截止时间
            entity = new CourseDiscussEntity();
            entity.setId(old.getId());
            entity.setEndTime(dto.getEndTime());
        } else {
            if (Objects.isNull(dto.getChapterId())) {
                dto.setChapterId(0L);
            }
            entity = courseDiscussConvert.update(dto);
            //定时发布
            if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getTiming())) {
                CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto.getReleaseTime()), MessageUtils.message("course.discuss.releasetime.not.blank"));
                //校验定时发布的发布时间在截止时间之前
                CommonErrors.BAD_REQUEST.check(now.isBefore(dto.getReleaseTime()), MessageUtils.message("course.discuss.releasetime.before"));
                CommonErrors.BAD_REQUEST.check(dto.getReleaseTime().isBefore(dto.getEndTime()), MessageUtils.message("course.discuss.releasetime.before"));
                entity.setNowRelease(YesNoNumberEnum.NO.getCode());
                message = MessageUtils.message("course.timing.message.success");
            }
            //非立即发布 -> 立即发布
            if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getNowRelease()) && Objects.equals(YesNoNumberEnum.NO.getCode(), old.getNowRelease())) {
                //发布时校验当前时间在截止时间之前
                CommonErrors.BAD_REQUEST.check(now.isBefore(dto.getEndTime()), MessageUtils.message("course.discuss.now.before"));
                entity.setStatus(YesNoNumberEnum.YES.getCode());
                entity.setReleaseTime(now);
                entity.setTiming(YesNoNumberEnum.NO.getCode());
                //同步班级设置
                entity.setCreateBy(old.getCreateBy());
                entity.setUpdateBy(old.getUpdateBy());
                this.synchroClass(entity);
                message = MessageUtils.message("course.release.message.success");
            }
        }
        this.updateById(entity);
        this.synchroContentDiscuss(courseDiscussConvert.convert(entity));
        // 发送消息助手
        if (Objects.equals(YesNoNumberEnum.NO.getCode(), old.getStatus()) && Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getNowRelease()) && Objects.equals(YesNoNumberEnum.NO.getCode(), old.getNowRelease())) {
            assistantListen.releaseCourseContent(entity.getId(), AssistantMessageTypeEnum.DISCUSS);
        }
        return message;
    }

    /**
     * 删除讨论
     *
     * @Title:
     * @MethodName: removeById
     * [id, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 15:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeById(Long id, Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.discuss.id.not.blank"));
        CourseDiscussEntity entity = Optional.ofNullable(this.getById(id)).orElseThrow(() -> CommonErrors.NOT_DATA.asException(MessageUtils.message("course.discuss.is.remove")));

        // 校验板块是否被移除
        classMenuClient.checkShowCourseContent(entity.getClassId(), CourseContentTypeEnum.DISCUSS);

        ClassStudentResultDTO studentResult = R.as(classStudentClient.checkRole(entity.getClassId(), userId, new ArrayList<>()));
        boolean check = checkRemove(entity, studentResult);
        CommonErrors.BAD_REQUEST.check(check, MessageUtils.message("course.comment.is.not.permissions"));
        //同步删除主表
        courseContentClient.deleteCourseContentByIds(new ArrayList<>(Collections.singletonList(id)), CourseContentTypeEnum.DISCUSS.getCode());
        //同步删除学生记录表
        courseDiscussStudentLogService.deleteByContentId(id);

        return this.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @Title:
     * @MethodName: removeByIds
     * [dto]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/15 16:38
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeByIds(CourseDiscussDeleteDTO dto) {
        CommonErrors.BAD_REQUEST.check(CollectionUtils.isNotEmpty(dto.getIds()), MessageUtils.message("course.discuss.ids.not.blank"));
        List<CourseDiscussEntity> list = this.baseMapper.selectBatchIds(dto.getIds());
        if (CollectionUtils.isEmpty(list)) {
            return Boolean.TRUE;
        }
        Long classId = list.get(0).getClassId();
        ClassStudentResultDTO studentResult = R.as(classStudentClient.checkRole(classId, dto.getUserId(), new ArrayList<>()));
        boolean check = Boolean.TRUE;
        for (CourseDiscussEntity entity : list) {
            if (!Objects.equals(classId, entity.getClassId())) {
                check = Boolean.FALSE;
            } else {
                check = checkRemove(entity, studentResult);
            }
            if (!check) {
                break;
            }
        }
        CommonErrors.BAD_REQUEST.check(check, MessageUtils.message("course.comment.is.not.permissions"));

        //同步删除主表
        courseContentClient.deleteCourseContentByIds(dto.getIds(), CourseContentTypeEnum.DISCUSS.getCode());
        //同步删除学生记录表
        courseDiscussStudentLogService.deleteByContentIds(dto.getIds());

        return this.deletedByIds(dto.getIds());
    }

    /**
     * 设置精华
     *
     * @Title:
     * @MethodName: isSupper
     * [id, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 19:36
     */
    @Override
    public Boolean isSupper(Long id, Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.discuss.id.not.blank"));
        CourseDiscussEntity entity = Optional.ofNullable(this.getById(id)).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));

        classMenuClient.checkShowCourseContent(entity.getClassId(), CourseContentTypeEnum.DISCUSS);

        //校验为本班老师
        List<ClassRoleEnum> classRoles = new ArrayList<>();
        classRoles.add(ClassRoleEnum.TEACHER);
        classRoles.add(ClassRoleEnum.ASSISTANT);
        classStudentClient.checkRole(entity.getClassId(), userId, classRoles);
        //未发布不可操作
        CommonErrors.BAD_REQUEST.check(Objects.equals(YesNoNumberEnum.YES.getCode(), entity.getStatus()), MessageUtils.message("course.discuss.message.fail"));
        //精华限制 -- 10条
        LambdaQueryWrapper<CourseDiscussEntity> queryWrapper = Queries.lambda();
        queryWrapper.eq(CourseDiscussEntity::getClassId, entity.getClassId()).eq(CourseDiscussEntity::getIsSuper, YesNoNumberEnum.YES.getCode());
        CommonErrors.BAD_REQUEST.check(this.baseMapper.selectCount(queryWrapper) <= CourseConstants.CourseDiscussCount.SUPPER_NUM, MessageUtils.message("course.discuss.supper.valid"));

        entity.setIsSuper(YesNoNumberEnum.YES.getCode());
        return this.updateById(entity);
    }

    /**
     * 取消精华
     *
     * @Title:
     * @MethodName: cancelSuper
     * [id, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 19:36
     */
    @Override
    public Boolean cancelSuper(Long id, Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.discuss.id.not.blank"));
        CourseDiscussEntity entity = Optional.ofNullable(this.getById(id)).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));

        classMenuClient.checkShowCourseContent(entity.getClassId(), CourseContentTypeEnum.DISCUSS);

        //校验为本班老师
        List<ClassRoleEnum> classRoles = new ArrayList<>();
        classRoles.add(ClassRoleEnum.TEACHER);
        classRoles.add(ClassRoleEnum.ASSISTANT);
        classStudentClient.checkRole(entity.getClassId(), userId, classRoles);

        entity.setIsSuper(YesNoNumberEnum.NO.getCode());
        entity.setId(id);
        return this.updateById(entity);
    }

    /**
     * 置顶
     *
     * @Title:
     * @MethodName: isTop
     * [id, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 19:36
     */
    @Override
    public Boolean isTop(Long id, Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.discuss.id.not.blank"));
        CourseDiscussEntity entity = Optional.ofNullable(this.getById(id)).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));

        // 校验板块是否被移除
        classMenuClient.checkShowCourseContent(entity.getClassId(), CourseContentTypeEnum.DISCUSS);

        //校验为本班老师
        List<ClassRoleEnum> classRoles = new ArrayList<>();
        classRoles.add(ClassRoleEnum.TEACHER);
        classRoles.add(ClassRoleEnum.ASSISTANT);
        classStudentClient.checkRole(entity.getClassId(), userId, classRoles);
        //未发布不可操作
        CommonErrors.BAD_REQUEST.check(Objects.equals(YesNoNumberEnum.YES.getCode(), entity.getStatus()), MessageUtils.message("course.discuss.message.fail"));
        //置顶限制 -- 10条
        LambdaQueryWrapper<CourseDiscussEntity> queryWrapper = Queries.lambda();
        queryWrapper.eq(CourseDiscussEntity::getClassId, entity.getClassId()).eq(CourseDiscussEntity::getIsTop, YesNoNumberEnum.YES.getCode());
        CommonErrors.BAD_REQUEST.check(this.baseMapper.selectCount(queryWrapper) <= CourseConstants.CourseDiscussCount.TOP_NUM, MessageUtils.message("course.discuss.top.valid"));

        entity.setIsTop(YesNoNumberEnum.YES.getCode());
        entity.setTopTime(LocalDateTime.now());
        return this.updateById(entity);
    }

    /**
     * 取消置顶
     *
     * @Title:
     * @MethodName: cancelTop
     * [id, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 19:36
     */
    @Override
    public Boolean cancelTop(Long id, Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.discuss.id.not.blank"));
        CourseDiscussEntity entity = Optional.ofNullable(this.getById(id)).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));

        classMenuClient.checkShowCourseContent(entity.getClassId(), CourseContentTypeEnum.DISCUSS);

        //校验为本班老师
        List<ClassRoleEnum> classRoles = new ArrayList<>();
        classRoles.add(ClassRoleEnum.TEACHER);
        classRoles.add(ClassRoleEnum.ASSISTANT);
        classStudentClient.checkRole(entity.getClassId(), userId, classRoles);

        LambdaUpdateWrapper<CourseDiscussEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CourseDiscussEntity::getIsTop, YesNoNumberEnum.NO.getCode()).set(CourseDiscussEntity::getTopTime, null).eq(CourseDiscussEntity::getId, id);

        return this.update(wrapper);
    }

    /**
     * 发布
     *
     * @Title:
     * @MethodName: isRelease
     * [id, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 19:36
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean isRelease(Long id, Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("course.discuss.id.not.blank"));
        CourseDiscussEntity entity = Optional.ofNullable(this.getById(id)).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));

        classMenuClient.checkShowCourseContent(entity.getClassId(), CourseContentTypeEnum.DISCUSS);

        //校验为本班老师
        List<ClassRoleEnum> classRoles = new ArrayList<>();
        classRoles.add(ClassRoleEnum.TEACHER);
        classRoles.add(ClassRoleEnum.ASSISTANT);
        classStudentClient.checkRole(entity.getClassId(), userId, classRoles);
        //未发布才可以发布
        CommonErrors.BAD_REQUEST.check(Objects.equals(YesNoNumberEnum.NO.getCode(), entity.getStatus()), MessageUtils.message("course.discuss.is.not.unpublished"));
        LocalDateTime now = LocalDateTime.now();
        //发布时校验当前时间在截止时间之前
        CommonErrors.BAD_REQUEST.check(now.isBefore(entity.getEndTime()), MessageUtils.message("course.discuss.now.before"));

        entity.setStatus(YesNoNumberEnum.YES.getCode());
        entity.setReleaseTime(now);
        //定时发布一键发布
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), entity.getTiming())) {
            entity.setTiming(YesNoNumberEnum.NO.getCode());
        }
        boolean flag = this.updateById(entity);

        if (flag) {
            //同步班级设置
            this.synchroClass(entity);
            this.synchroContentDiscuss(courseDiscussConvert.convert(entity));
            //TODO 发送新奇助手消息
            assistantListen.releaseCourseContent(entity.getId(), AssistantMessageTypeEnum.DISCUSS);
        }
        return flag;
    }

    /**
     * 发布V2版本
     *
     * @param id
     * @param userId 操作人
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean isReleaseV2(Long id, Long userId) {

        CourseDiscussEntity entity = Optional.ofNullable(this.getById(id)).orElseThrow(() -> CommonErrors.BAD_REQUEST.asException(MessageUtils.message("course.discuss.is.remove")));
        CommonErrors.BAD_REQUEST.check(Objects.equals(YesNoNumberEnum.NO.getCode(), entity.getStatus()),
            MessageUtils.message("course.discuss.release.valid"));

        classMenuClient.checkShowCourseContent(entity.getClassId(), CourseContentTypeEnum.DISCUSS);

        //校验为本班老师
        List<ClassRoleEnum> classRoles = new ArrayList<>();
        classRoles.add(ClassRoleEnum.TEACHER);
        classRoles.add(ClassRoleEnum.ASSISTANT);
        classStudentClient.checkRole(entity.getClassId(), userId, classRoles);
        //未发布才可以发布
        CommonErrors.BAD_REQUEST.check(Objects.equals(YesNoNumberEnum.NO.getCode(), entity.getStatus()), MessageUtils.message("course.discuss.is.not.unpublished"));
        LocalDateTime now = LocalDateTime.now();
        //发布时校验当前时间在截止时间之前
        CommonErrors.BAD_REQUEST.check(now.isBefore(entity.getEndTime()), MessageUtils.message("course.discuss.now.before"));

        entity.setStatus(YesNoNumberEnum.YES.getCode());
        entity.setReleaseTime(now);
        //定时发布一键发布
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), entity.getTiming())) {
            entity.setTiming(YesNoNumberEnum.NO.getCode());
        }
        boolean flag = this.updateById(entity);

        if (flag) {
            //同步班级设置
            this.synchroClass(entity);
            this.synchroContentDiscuss(courseDiscussConvert.convert(entity));
            //TODO 发送新奇助手消息
            assistantListen.releaseCourseContent(entity.getId(), AssistantMessageTypeEnum.DISCUSS);
        }
        return flag;
    }

    /**
     * 已参与列表/未参与列表
     *
     * @param pageable
     * @param query
     * @return
     */
    @Override
    public PageInfo<CourseDiscussStudentLogResultDTO> selectStudentLogPage(Param pageable, CourseDiscussStudentLogQueryDTO query) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(query.getContentId()), MessageUtils.message("course.discuss.id.not.blank"));
        return courseDiscussStudentLogService.selectPage(pageable, query);
    }

    /**
     * 讨论统计
     *
     * @Title:
     * @MethodName: selectStatisticsPage
     * [pageable, query]
     * @Return com.xinqi.common.base.response.PageInfo<com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussStatisticsResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/16 15:54
     */
    @Override
    @DS("slaver")
    public PageInfo<CourseDiscussStatisticsResultDTO> selectStatisticsPage(Param pageable, CourseDiscussStatisticsQueryDTO query) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(query.getClassId()), MessageUtils.message("course.discuss.classId.not.blank"));
        IPage<CourseDiscussStatisticsResultDTO> pageData = this.baseMapper.selectStatisticsPage(Pages.page(pageable), query);
        return Pages.convert(pageData);
    }

    /**
     * 下载
     *
     * @Title:
     * @MethodName: download
     * [dto]
     * @Return java.util.List<java.lang.String>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/20 17:07
     */
    @Override
    public List<String> download(CourseDiscussDownloadDto dto) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto.getIds()), MessageUtils.message("course.discuss.ids.not.blank"));
        List<String> urls = new ArrayList<>();
        List<CourseDiscussEntity> entityList = this.baseMapper.selectBatchIds(dto.getIds());
        if (entityList != null && entityList.size() > 0) {
            for (CourseDiscussEntity entity : entityList) {
                if (entity.getFileurls() != null && StringUtils.isNotEmpty(entity.getFileurls())) {
                    List<CommonFileDTO> files = JSONArray.parseArray(entity.getFileurls(), CommonFileDTO.class);
                    if (files != null && files.size() > 0) {
                        files.forEach(item -> urls.add(item.getFileUrl()));
                    }
                }
                if (entity.getImages() != null && StringUtils.isNotEmpty(entity.getImages())) {
                    List<CommonFileDTO> images = JSONArray.parseArray(entity.getImages(), CommonFileDTO.class);
                    if (images != null && images.size() > 0) {
                        images.forEach(item -> urls.add(item.getFileUrl()));
                    }
                }
                if (entity.getVideos() != null && StringUtils.isNotEmpty(entity.getVideos())) {
                    List<CommonFileDTO> files = JSONArray.parseArray(entity.getVideos(), CommonFileDTO.class);
                    if (files != null && files.size() > 0) {
                        files.forEach(item -> urls.add(item.getFileUrl()));
                    }
                }
            }
        }
        return urls;
    }


    /**
     * 定时发布
     *
     * @Title:
     * @MethodName: releaseDiscuss
     * @Return boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/21 17:57
     */
    @Override
    public boolean releaseDiscuss() {
        //查询定时未发布
        LambdaUpdateWrapper<CourseDiscussEntity> query = new LambdaUpdateWrapper<>();
        query.eq(CourseDiscussEntity::getTiming, YesNoNumberEnum.YES.getCode()).eq(CourseDiscussEntity::getStatus, YesNoNumberEnum.NO.getCode()).le(CourseDiscussEntity::getReleaseTime, LocalDateTime.now());
        List<CourseDiscussEntity> list = this.baseMapper.selectList(query);
        // 没有发布中的数据直接返回true
        if (CollectionUtils.isEmpty(list)) {
            return true;
        }
        //筛选需要发布
        List<Long> ids = new ArrayList<>();
        List<CourseDiscussEntity> releaseList = new ArrayList<>();
        list.forEach(item -> {
            ids.add(item.getId());
            item.setStatus(YesNoNumberEnum.YES.getCode());
            releaseList.add(item);

        });

        if (CollectionUtils.isNotEmpty(ids)) {
            //同步主表
            courseContentClient.update(YesNoNumberEnum.YES.getCode(), CourseContentTypeEnum.DISCUSS.getCode(), ids);
            //发布
            LambdaUpdateWrapper<CourseDiscussEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(CourseDiscussEntity::getStatus, YesNoNumberEnum.YES.getCode()).in(CourseDiscussEntity::getId, ids);
            this.update(wrapper);
            //TODO 新奇助手发送讨论
            for (Long contentId : ids) {
                assistantListen.releaseCourseContent(contentId, AssistantMessageTypeEnum.DISCUSS);
            }
        }
        //同步班级设置
        synchroClass(releaseList);
        return true;
    }

    /**
     * 更具ID获取讨论信息
     * @param id
     * @return
     */
    @Override
    public CourseDiscussResultDTO findById(Long id) {
        CourseDiscussEntity entity = this.baseMapper.selectById(id);
        if (Objects.nonNull(entity)) {
            return courseDiscussConvert.convert(entity);
        } else {
            return null;
        }
    }

    /**
     * 班级添加成员 -- 讨论同步
     *
     * @Title:
     * @MethodName: classAddMembers
     * [classId, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/30 13:47
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean classAddMembers(Long classId, Long userId) {

        //查询未发布
        LambdaQueryWrapper<CourseDiscussEntity> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(CourseDiscussEntity::getClassId, classId).eq(CourseDiscussEntity::getStatus, YesNoNumberEnum.NO.getCode());
        List<CourseDiscussEntity> unpublished = this.baseMapper.selectList(wrapper1);
        //查询进行中
        List<CourseDiscussEntity> inProgress = this.baseMapper.selectList(wrapper(classId));

        List<CourseDiscussEntity> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(unpublished)) {
            list.addAll(unpublished);
        }
        if (CollectionUtils.isNotEmpty(inProgress)) {
            list.addAll(inProgress);
        }
        if (CollectionUtils.isEmpty(list)) {
            return true;
        }
        courseDiscussStudentLogService.synchroStudent(list, classId, userId);
        return true;
    }

    /**
     * 班级退出成员 -- 讨论同步
     *
     * @Title:
     * @MethodName: classRemoveMembers
     * [classId, userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/30 13:47
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean classRemoveMembers(Long classId, Long userId) {
        //查询未发布
        LambdaQueryWrapper<CourseDiscussEntity> query = new LambdaQueryWrapper<>();
        query.eq(CourseDiscussEntity::getClassId, classId).eq(CourseDiscussEntity::getStatus, YesNoNumberEnum.NO.getCode());
        List<CourseDiscussEntity> list = this.baseMapper.selectList(query);
        if (CollectionUtils.isEmpty(list)) {
            return true;
        }
        List<Long> idList = list.stream().map(CourseDiscussEntity::getId).collect(Collectors.toList());

        LambdaQueryWrapper<CourseDiscussStudentLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseDiscussStudentLogEntity::getStudentId, userId).in(CourseDiscussStudentLogEntity::getContentId, idList);

        return courseDiscussStudentLogService.remove(wrapper);
    }

    /**
     * 讨论参与未参与统计
     */
    @Override
    public AssistantTotalResultDTO selectDiscussTakeTotal(Long contentId) {
        return this.baseMapper.selectDiscussTakeTotal(contentId);
    }

    /**
     * 讨论参与未参与学生列表
     */
    @Override
    public List<StudentInfoResultDTO> selectDiscussTakeStudent(Long contentId, Integer isPartake) {
        return this.baseMapper.selectDiscussTakeStudent(contentId, isPartake);
    }

    /**
     * 讨论进行中列表查询
     */
    @Override
    public List<CourseDiscussEntity> selectDiscussRunningList(Long classId) {
        return this.baseMapper.selectList(wrapper(classId));
    }

    @Override
    public void participate(Long classId, Long contentId, Long userId) {
        CourseDiscussEntity discuss = this.baseMapper.selectById(contentId);
        if (Objects.isNull(discuss)) {
            return;
        }
        if (Objects.isNull(discuss.getReleaseTime())) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        if (now.isAfter(discuss.getReleaseTime()) && now.isBefore(discuss.getEndTime())) {
            // 获取发布用户信息
            ClassStudentResultDTO publishStudent = R.as(classStudentClient.findByStudentId(classId, discuss.getCreateBy()));
            if (Objects.isNull(publishStudent)) {
                return;
            }

            // 获取 主学生 信息
            ClassStudentResultDTO mainStudent = R.as(classStudentClient.findVirtualClassStudentByClassIdAndUserId(classId, userId));

            // 判断发布人和参与人是否是同一用户
            if (Objects.isNull(mainStudent)) {
                if (Objects.equals(publishStudent.getStudentId(), userId)) {
                    return;
                }
            } else {
                // 获取发布人 主学生信息
                ClassStudentResultDTO publishMain = R.as(classStudentClient.findVirtualClassStudentByClassIdAndUserId(classId, discuss.getCreateBy()));
                if (Objects.nonNull(publishMain) && Objects.equals(publishMain.getStudentId(), mainStudent.getStudentId())) {
                    return;
                }
            }

            CourseDiscussStudentLogEntity studentLogEntity = null;
            if (Objects.nonNull(mainStudent)) {
                studentLogEntity = courseDiscussStudentLogService.selectByContentIdAndStudentId(contentId, mainStudent.getStudentId());
            } else {
                studentLogEntity = courseDiscussStudentLogService.selectByContentIdAndStudentId(contentId, userId);
            }
            if (Objects.isNull(studentLogEntity)) {
                //无数据 -- 未读/未参与
                studentLogEntity = new CourseDiscussStudentLogEntity();
                studentLogEntity.setClassId(classId);
                studentLogEntity.setContentId(contentId);
                studentLogEntity.setStudentId(Objects.nonNull(mainStudent) ? mainStudent.getStudentId() : userId);
                studentLogEntity.setIsRead(YesNoNumberEnum.YES.getCode());
                studentLogEntity.setIsPartake(YesNoNumberEnum.YES.getCode());

                courseDiscussStudentLogService.save(studentLogEntity);
            } else if (Objects.equals(YesNoNumberEnum.NO.getCode(), studentLogEntity.getIsPartake())) {
                //有数据 -- 未参与
                studentLogEntity.setIsRead(YesNoNumberEnum.YES.getCode());
                studentLogEntity.setIsPartake(YesNoNumberEnum.YES.getCode());

                courseDiscussStudentLogService.updateById(studentLogEntity);
            }
        }
    }

    /**
     * 进行中的查询
     *
     * @param classId
     * @return
     */
    private LambdaQueryWrapper<CourseDiscussEntity> wrapper(Long classId) {
        LambdaQueryWrapper<CourseDiscussEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseDiscussEntity::getClassId, classId).eq(CourseDiscussEntity::getStatus, YesNoNumberEnum.YES.getCode()).gt(CourseDiscussEntity::getEndTime, LocalDateTime.now());
        return wrapper;
    }

    /**
     * 同步班级
     *
     * @param item
     */
    private void synchroClass(CourseDiscussEntity item) {
        if (Objects.isNull(item)) {
            return;
        }
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), item.getSynchro())) {
            if (StringUtils.isNotEmpty(item.getClassIds())) {
                List<CourseDiscussEntity> list = new ArrayList<>();
                String[] classIds = item.getClassIds().split(",");
                for (String classId : classIds) {
                    CourseDiscussEntity entity = courseDiscussConvert.newEntity(item);
                    entity.setId(null);
                    entity.setChapterId(null);
                    entity.setClassId(Long.valueOf(classId));
                    entity.setClassIds(StringUtils.EMPTY);
                    entity.setSynchro(YesNoNumberEnum.NO.getCode());
                    entity.setCreateBy(item.getCreateBy());
                    list.add(entity);
                }
                if (CollectionUtils.isNotEmpty(list)) {
                    this.saveBatch(list);
                    // 同步学生设置
                    courseDiscussStudentLogService.synchroStudent(list);
                    this.synchroContentDiscuss(list.stream().map(courseDiscussConvert::convert).collect(Collectors.toList()));
                    // 发布助手通知
                    if (Objects.equals(YesNoNumberEnum.YES.getCode(), list.get(0).getStatus())) {
                        Spring.committed(() -> list.forEach(discussEntity -> assistantListen.releaseCourseContent(discussEntity.getId(), AssistantMessageTypeEnum.DISCUSS)));
                    }
                }
            }
        }
    }

    /**
     * 同步班级
     *
     * @param discussList
     */
    private void synchroClass(List<CourseDiscussEntity> discussList) {
        if (CollectionUtils.isEmpty(discussList)) {
            return;
        }
        List<CourseDiscussEntity> list = new ArrayList<>();
        discussList.forEach(item -> {
            if (Objects.equals(YesNoNumberEnum.YES.getCode(), item.getSynchro())) {
                if (StringUtils.isNotEmpty(item.getClassIds())) {
                    String[] classIds = item.getClassIds().split(",");
                    for (String classId : classIds) {
                        CourseDiscussEntity entity = courseDiscussConvert.newEntity(item);
                        entity.setId(null);
                        entity.setChapterId(null);
                        entity.setClassId(Long.valueOf(classId));
                        entity.setClassIds(StringUtils.EMPTY);
                        entity.setSynchro(YesNoNumberEnum.NO.getCode());
                        entity.setCreateBy(item.getCreateBy());
                        list.add(entity);
                    }
                }
            }
        });
        if (CollectionUtils.isNotEmpty(list)) {
            this.saveBatch(list);
            //同步学生设置
            courseDiscussStudentLogService.synchroStudent(list);
            this.synchroContentDiscuss(list.stream().map(courseDiscussConvert::convert).collect(Collectors.toList()));
            //TODO
            list.forEach(entity -> {
                if (Objects.equals(YesNoNumberEnum.YES.getCode(), entity.getStatus())) {
                    //发送消息助手
                    assistantListen.releaseCourseContent(entity.getId(), AssistantMessageTypeEnum.DISCUSS);
                }
            });
        }
    }

    //TODO

    /**
     * 删除权限校验
     *
     * @param entity
     * @param studentResult
     */
    private Boolean checkRemove(CourseDiscussEntity entity, ClassStudentResultDTO studentResult) {
        if (Objects.equals(ClassRoleEnum.STUDENT.getCode(), studentResult.getClassRole())) {
            //学生只可以删除自己的
            return Objects.equals(studentResult.getStudentId(), entity.getCreateBy());
        } else if (Objects.equals(ClassRoleEnum.REPRESENTATIVE.getCode(), studentResult.getClassRole())) {
            //课代表（学生）只可以删除自己的
            return Objects.equals(studentResult.getStudentId(), entity.getCreateBy());
        } else if (Objects.equals(ClassRoleEnum.ASSISTANT.getCode(), studentResult.getClassRole())) {
            //助教可以对班级所有人的进行删除
            return true;
        } else if (Objects.equals(ClassRoleEnum.TEACHER.getCode(), studentResult.getClassRole())) {
            //老师可以对班级所有人的进行删除
            return true;
        } else {
            return false;
        }
    }


    /**
     * 同步主表 -- 讨论
     *
     * @param discuss
     */
    @Override
    public void synchroContentDiscuss(CourseDiscussResultDTO discuss) {
        CourseContentCreateDTO create = new CourseContentCreateDTO();
        //存在同步到其他班级，属于同课程同步，操作人都是当前用户，即班级ID跟随讨论，学校课程则是固定的
        create.setUserId(discuss.getCreateBy());
        create.setClassId(discuss.getClassId());
        create.setContentId(discuss.getId());
        create.setType(CourseContentTypeEnum.DISCUSS.getCode());
        create.setTitle(discuss.getTitle());
        create.setContent(discuss.getContent());
        create.setChapterId(discuss.getChapterId());
        create.setReleaseTime(discuss.getReleaseTime());
        create.setStatus(discuss.getStatus());
        create.setEndTime(discuss.getEndTime());
        CourseContentResultDTO checkContent = R.as(courseContentClient.selectByContentIdAndType(discuss.getId(), CourseContentTypeEnum.DISCUSS.getCode()));
        if (Objects.isNull(checkContent)) {
            courseContentClient.saveContent(create);
        } else {
            CourseContentUpdateDTO update = new CourseContentUpdateDTO();
            //存在同步到其他班级，属于同课程同步，操作人都是当前用户，即班级ID跟随讨论，学校课程则是固定的
            update.setUserId(discuss.getCreateBy());
            update.setClassId(discuss.getClassId());
            update.setContentId(discuss.getId());
            update.setType(CourseContentTypeEnum.DISCUSS.getCode());
            update.setTitle(discuss.getTitle());
            update.setContent(discuss.getContent());
            update.setChapterId(discuss.getChapterId());
            update.setReleaseTime(discuss.getReleaseTime());
            update.setStatus(discuss.getStatus());
            update.setEndTime(discuss.getEndTime());
            update.setId(checkContent.getId());
            courseContentClient.updateById(checkContent.getId(), update);
        }
    }

    /**
     * 同步主表 -- 讨论
     *
     * @param discussList
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void synchroContentDiscuss(List<CourseDiscussResultDTO> discussList) {
        if (CollectionUtils.isEmpty(discussList)) {
            return;
        }
        List<CourseContentCreateDTO> saveList = new ArrayList<>();
        List<CourseContentUpdateDTO> updateList = new ArrayList<>();

        List<Long> contentIds = discussList.stream().map(CourseDiscussResultDTO::getId).collect(Collectors.toList());

        List<CourseContentResultDTO> contentResultList = R.as(courseContentClient.findContentListByIdsAndType(contentIds, CourseContentTypeEnum.DISCUSS.getCode()));
        Map<Long, CourseContentResultDTO> entityMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(contentResultList)) {
            contentResultList.forEach(entity -> {
                entityMap.put(entity.getContentId(), entity);
            });
        }

        discussList.forEach(discuss -> {
            CourseContentCreateDTO create = new CourseContentCreateDTO();
            create.setUserId(discuss.getCreateBy());
            create.setClassId(discuss.getClassId());
            create.setContentId(discuss.getId());
            create.setType(CourseContentTypeEnum.DISCUSS.getCode());
            create.setTitle(discuss.getTitle());
            create.setContent(discuss.getContent());
            create.setChapterId(discuss.getChapterId());
            create.setReleaseTime(discuss.getReleaseTime());
            create.setStatus(discuss.getStatus());
            create.setEndTime(discuss.getEndTime());

            CourseContentResultDTO checkContent = entityMap.get(discuss.getId());
            if (Objects.nonNull(checkContent)) {
                CourseContentUpdateDTO update = new CourseContentUpdateDTO();
                update.setUserId(checkContent.getUserId());
                update.setContent(checkContent.getContent());
                update.setTitle(checkContent.getTitle());
                update.setType(CourseContentTypeEnum.DISCUSS.getCode());
                update.setClassId(checkContent.getClassId());
                update.setChapterId(checkContent.getChapterId());
                update.setStatus(YesNoNumberEnum.YES.getCode());
                update.setReleaseTime(checkContent.getReleaseTime());
                update.setId(checkContent.getId());
                updateList.add(update);
            } else {
                saveList.add(create);
            }
        });
        if (CollectionUtils.isNotEmpty(saveList)) {
            courseContentClient.saveBatchContent(saveList);
        }
        if (CollectionUtils.isNotEmpty(updateList)) {
            courseContentClient.updateBatch(updateList);
        }
    }


}
